package com.wr.recruit.service.apis;

import com.google.common.collect.Lists;
import com.wr.recruit.service.apis.dto.ApplyMailDto;
import com.wr.recruit.service.dao.dbo.CompanyDbo;
import com.wr.recruit.service.dao.dbo.MailDbo;
import com.wr.recruit.service.dao.dbo.UserDbo;
import com.wr.recruit.service.service.CompanyService;
import com.wr.recruit.service.service.MailService;
import com.wr.recruit.service.service.UserService;
import com.wr.recruit.service.utils.APIResponse;
import com.wr.recruit.service.utils.APIResponseSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@Controller
public class MailApis {

    private MailService mailService;
    private UserService userService;
    private CompanyService companyService;

    @Autowired
    public MailApis(MailService mailService,
                    UserService userService,
                    CompanyService companyService) {
        this.mailService = mailService;
        this.userService = userService;
        this.companyService = companyService;
    }

    @PostMapping(value = "/mail/commit", produces = "application/json")
    @ResponseBody
    public APIResponse sendMailToInvestigator(@RequestBody ApplyMailDto applyMailDto){
        UserDbo user = userService.getUserInfo(applyMailDto.getApplicant());
        String subject = user.getTrueName() + "的面试申请";
        String content = "系统提醒：请在收到邮件后一周内回复";
        String mailCode = UUID.randomUUID().toString();
        try {
            String info = mailService.sendMail(mailCode, 1, applyMailDto.getApplicant(),
                    applyMailDto.getInvestigator(), user.getAttachmentCode(), subject, content);
            return new APIResponseSet(true,"发送邮件成功", Lists.newArrayList(info));
        } catch (Exception e) {
            return new APIResponseSet(false,"发送邮件失败", Lists.newArrayList(e.getMessage()));
        }
    }

    @PostMapping(value = "/mail/investigate", produces = "application/json")
    @ResponseBody
    public APIResponse sendMailToInterviewer(@RequestBody ApplyMailDto applyMailDto){
        MailDbo mailInfo = mailService.getMailInfo(applyMailDto.getMailCode(),1);

        String subject = userService.getUserInfo(mailInfo.getSender()).getTrueName() + "的面试安排";
        String content = userService.getUserInfo(applyMailDto.getInterviewer()).getTrueName() + "您有一场面试，请登陆系统，选择面试时间";
        try {
            String info = mailService.sendMail(applyMailDto.getMailCode(), 2, mailInfo.getAccepter(),
                    applyMailDto.getInterviewer(), mailInfo.getAttachmentCode(), subject, content);
            return new APIResponseSet(true,"发送邮件成功", Lists.newArrayList(info));
        } catch (Exception e) {
            return new APIResponseSet(false,"发送邮件失败", Lists.newArrayList(e.getMessage()));
        }
    }

    @PostMapping(value = "/mail/interviewer", produces = "application/json")
    @ResponseBody
    public APIResponse sendMailToApplicant(@RequestBody ApplyMailDto applyMailDto){
        MailDbo mailInfo = mailService.getMailInfo(applyMailDto.getMailCode(),1);
        UserDbo user = userService.getUserInfo(mailInfo.getSender());

        UserDbo interviewer = userService.getUserInfo(mailInfo.getAccepter());
        CompanyDbo companyInfo = companyService.getCompanyInfoByCompanyCode(interviewer.getCompanyCode());
        String subject = user.getTrueName() + "的面试安排";
        String content = user.getTrueName() + "您有一场面试，请在" + applyMailDto.getTime() + "于" + companyInfo.getCompanyAddress() + "参与面试" ;

        MailDbo mailInfoTwo = mailService.getMailInfo(applyMailDto.getMailCode(),2);
        try {
            String info = mailService.sendMail(applyMailDto.getMailCode(), 3, mailInfoTwo.getAccepter(),
                    user.getUserName(), mailInfo.getAttachmentCode(), subject, content);
            return new APIResponseSet(true,"发送邮件成功", Lists.newArrayList(info));
        } catch (Exception e) {
            return new APIResponseSet(false,"发送邮件失败", Lists.newArrayList(e.getMessage()));
        }
    }

    @PostMapping(value = "/mail/result/interview", produces = "application/json")
    @ResponseBody
    public APIResponse sendResultToInterviewer(@RequestBody ApplyMailDto applyMailDto){
        MailDbo mailInfo = mailService.getMailInfo(applyMailDto.getMailCode(),1);
        UserDbo user = userService.getUserInfo(mailInfo.getSender());

        String subject = user.getTrueName() + "的面试结果";
        String content = user.getTrueName() + "的成绩为：[" + applyMailDto.getScore() + "]";

        MailDbo mailInfoTwo = mailService.getMailInfo(applyMailDto.getMailCode(),2);

        try {
            String info = mailService.sendMail(applyMailDto.getMailCode(), 4, mailInfoTwo.getAccepter(),
                    mailInfoTwo.getSender(), mailInfoTwo.getAttachmentCode(), subject, content);
            return new APIResponseSet(true,"发送邮件成功", Lists.newArrayList(info));
        } catch (Exception e) {
            return new APIResponseSet(false,"发送邮件失败", Lists.newArrayList(e.getMessage()));
        }
    }

    @PostMapping(value = "/mail/result/company", produces = "application/json")
    @ResponseBody
    public APIResponse sendResultToApplicant(@RequestBody ApplyMailDto applyMailDto){
        MailDbo mailInfo = mailService.getMailInfo(applyMailDto.getMailCode(),1);
        UserDbo user = userService.getUserInfo(mailInfo.getSender());

        UserDbo interviewer = userService.getUserInfo(mailInfo.getAccepter());
        CompanyDbo companyInfo = companyService.getCompanyInfoByCompanyCode(interviewer.getCompanyCode());
        String subject = user.getTrueName() + "的面试结果";
        String content = true ? user.getTrueName() + ",您已被我司录用，请在" + applyMailDto.getTime() + "前到"+ companyInfo.getCompanyAddress() + "入职。附件为入职相关文件"
                : user.getTrueName() + ",您未被我司录用,感谢您对我司的支持,谢谢";

        try {
            String info = mailService.sendMail(applyMailDto.getMailCode(), 5, mailInfo.getAccepter(),
                    mailInfo.getSender(), mailInfo.getAttachmentCode(), subject, content);
            return new APIResponseSet(true,"发送邮件成功", Lists.newArrayList(info));
        } catch (Exception e) {
            return new APIResponseSet(false,"发送邮件失败", Lists.newArrayList(e.getMessage()));
        }
    }

    @PostMapping(value = "/mail/confirm", produces = "application/json")
    @ResponseBody
    public APIResponse sendConfirmToInvestigator(@RequestBody ApplyMailDto applyMailDto){
        MailDbo mailInfo = mailService.getMailInfo(applyMailDto.getMailCode(),1);
        UserDbo user = userService.getUserInfo(mailInfo.getSender());

        UserDbo interviewer = userService.getUserInfo(mailInfo.getAccepter());
        CompanyDbo companyInfo = companyService.getCompanyInfoByCompanyCode(interviewer.getCompanyCode());
        String subject = user.getTrueName() + "确认入职邮件";
        String content = user.getTrueName() + "确认入职，会在规定时间内到" + companyInfo.getCompanyAddress() + "报道";

        try {
            String info = mailService.sendMail(applyMailDto.getMailCode(), 5, mailInfo.getSender(),
                    mailInfo.getAccepter(), mailInfo.getAttachmentCode(), subject, content);
            return new APIResponseSet(true,"发送邮件成功", Lists.newArrayList(info));
        } catch (Exception e) {
            return new APIResponseSet(false,"发送邮件失败", Lists.newArrayList(e.getMessage()));
        }
    }

    @GetMapping(value = "/mail", produces = "application/json")
    @ResponseBody
    public APIResponse getMailInfo(@RequestParam(value = "userName") String userName,
                                     @RequestParam(value = "pageSize") Integer pageSize,
                                     @RequestParam(value = "pageNum") Integer pageNum){
        Page<MailDbo> mailListInfo = mailService.getMailListInfo(userName, pageSize, pageNum);
        return new APIResponseSet(true,"查询邮件成功", Lists.newArrayList(mailListInfo));
    }
}
